<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @subpackage Demos
 * @copyright  Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * PHP sample code for the Google Calendar data API.  Utilizes the
 * Zend Framework Gdata components to communicate with the Google API.
 *
 * Requires the Zend Framework Gdata components and PHP >= 5.2.11
 *
 * You can run this sample both from the command line (CLI) and also
 * from a web browser.  When running through a web browser, only
 * AuthSub and outputting a list of calendars is demonstrated.  When
 * running via CLI, all functionality except AuthSub is available and dependent
 * upon the command line options passed.  Run this script without any
 * command line options to see usage, eg:
 *     /usr/local/bin/php -f Calendar.php
 *
 * More information on the Command Line Interface is available at:
 *     http://www.php.net/features.commandline
 *
 * NOTE: You must ensure that the Zend Framework is in your PHP include
 * path.  You can do this via php.ini settings, or by modifying the
 * argument to set_include_path in the code below.
 *
 * NOTE: As this is sample code, not all of the functions do full error
 * handling.  Please see getEvent for an example of how errors could
 * be handled and the online code samples for additional information.
 */

/**
 * @see Zend_Loader
 */
require_once 'Zend/Loader.php';

/**
 * @see Zend_Gdata
 */
Zend_Loader::loadClass('Zend_Gdata');

/**
 * @see Zend_Gdata_AuthSub
 */
Zend_Loader::loadClass('Zend_Gdata_AuthSub');

/**
 * @see Zend_Gdata_ClientLogin
 */
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

/**
 * @see Zend_Gdata_HttpClient
 */
Zend_Loader::loadClass('Zend_Gdata_HttpClient');

/**
 * @see Zend_Gdata_Calendar
 */
Zend_Loader::loadClass('Zend_Gdata_Calendar');

/**
 * @var string Location of AuthSub key file.  include_path is used to find this
 */
$_authSubKeyFile = null; // Example value for secure use: 'mykey.pem'

/**
 * @var string Passphrase for AuthSub key file.
 */
$_authSubKeyFilePassphrase = null;

/**
 * Returns the full URL of the current page, based upon env variables
 *
 * Env variables used:
 * $_SERVER['HTTPS'] = (on|off|)
 * $_SERVER['HTTP_HOST'] = value of the Host: header
 * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
 * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
 *
 * @return string Current URL
 */
//$startDate = $this->_getParam('start');
$startDate =  $_GET["start"];
   // $startDate = date("Y-m-d", $startDate);
    //$endDate = $this->_getParam('end');
    $endDate = $_GET["end"];
   // $endDate = date("Y-m-d", $endDate);
    // Remove the view & layout
   // $this->_helper->layout->disableLayout();
    //$this->_helper->viewRenderer->setNoRender(true);
    // Query Google GData for the calendar
    $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $source = "csl-test";
    //$source = "test google calendar";
    $user = "isbas.thecloud@gmail.com";
    $pass = "google1234567899";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service,null,$source);
   // echo $client;
	$cal = new Zend_Gdata_Calendar($client);
    $events = array();
    $query = $cal->newEventQuery();
   // $query->setUser('thecloudvisit@gmail.com');
    $query->setUser('isbas.thecloud@gmail.com');
    $query->setVisibility('private');
	$query->setProjection('full');
    $query->setStartMin($startDate);
    $query->setStartMax($endDate);
	$query->setsingleevents('true');
	//$echo $query;
    $eventFeed = $cal->getCalendarEventFeed($query);
    // Loop through the returned events:
	date_default_timezone_set('Asia/bangkok');
	
	//printR
	//$eventText=print_r( $eventFeed);
	//echo $eventText;
	
    foreach ($eventFeed as $event) 
    {
        $temp['id'] = $event->id->text;
        $temp['title'] = $event->title->text;
        $temp['detail'] = $event->Content->text;
        $temp['allDay'] = false;
        foreach ($event->when as $when) 
        {
            //$temp['start'] = date("D M j Y H:i:s eO", strtotime($when->startTime));
			
			$textCon_1=date("Y", strtotime($when->startTime));
			$textCon_2="-";
			$textCon_3=sprintf("%02s",date("m", strtotime($when->startTime)));
			$textCon_4="-";
			$textCon_5=sprintf("%02s",date("d", strtotime($when->startTime)));
			$textCon_6=" ";
			$textCon_7=sprintf("%02s",date("H", strtotime($when->startTime)));
			$textCon_8=":";
			$textCon_9=sprintf("%02s",date("i", strtotime($when->startTime)));
			$textCon_10=":";
			$textCon_11=sprintf("%02s",date("s", strtotime($when->startTime)));
			$textCon_12="+0700";
			
			//$textCon_1.$textCon_2.$textCon_3.$textCon_4.$textCon_5.$textCon_6.$textCon_7.$textCon_8.$textCon_9.$textCon_10.$textCon_11.$textCon_12
			
			
			
			
			
			
			
			
			
            $temp['start'] = $textCon_1.$textCon_2.$textCon_3.$textCon_4.$textCon_5.$textCon_6.$textCon_7.$textCon_8.$textCon_9.$textCon_10.$textCon_11.$textCon_12;
            
			
			
			$textCon_1=date("Y", strtotime($when->endTime));
			$textCon_2="-";
			$textCon_3=sprintf("%02s",date("m", strtotime($when->endTime)));
			$textCon_4="-";
			$textCon_5=sprintf("%02s",date("d", strtotime($when->endTime)));
			$textCon_6=" ";
			$textCon_7=sprintf("%02s",date("H", strtotime($when->endTime)));
			$textCon_8=":";
			$textCon_9=sprintf("%02s",date("i", strtotime($when->endTime)));
			$textCon_10=":";
			$textCon_11=sprintf("%02s",date("s", strtotime($when->endTime)));
			$textCon_12="+0700";
            //$temp['end'] = date("D M j Y H:i:s eO", strtotime($when->endTime));
			 $temp['end'] = $textCon_1.$textCon_2.$textCon_3.$textCon_4.$textCon_5.$textCon_6.$textCon_7.$textCon_8.$textCon_9.$textCon_10.$textCon_11.$textCon_12;
            
            //$temp['end'] = date("Y-m-d H:i:s+0700 ", strtotime($when->endTime));
			//if(date("H:i:s", strtotime($when->startTime))=="00:00:00")
			//	{
			//		//$temp['start'] = date("Y-m-d 24:00:00+07:00", strtotime($when->startTime));
			//	}

			
			//	{
			//		$temp['end'] = date("Y-m-d 23:00:00+07:00", strtotime($when->endTime));
			//	}
			
			$txtStTime=date("Y-m-d H:i:s", strtotime($when->startTime));
			// $temp['detail'] =substr($txtStTime,-8);
			$txtStTime=substr($txtStTime,-8);
			if($txtStTime=="00:00:00")
			{
				$temp['allDay']=true;
				$temp['start'] = date("Y-m-d", strtotime($when->startTime));
				$temp['end'] = date("Y-m", strtotime($when->endTime));
				$dayX=sprintf("%02s",date("d", strtotime($when->endTime)));
				//$temp['end'] =$temp['end']."-".$dayX." 00:00:00+0700 ";
				$temp['end'] =$temp['end']."-".$dayX;
			}
			//test firefox
			//$temp['allDay']=false;
			//$temp['start'] = ("2014-07-09 10:00:00+0700");
			//$temp['end'] = ("2014-07-09 12:00:00+0700");
		
        }  
		//$temp['end2'] = $event->endTime->text;
        array_push($events, $temp);
    }
   
    echo json_encode($events);
	?>